
def test():
    template_json_path = 'demo_qwenvl_clothing.json'
    with open(template_json_path, 'r', encoding='utf-8') as f:
        target_json_dict_str = f.read()

    non_empty_keys = ['basic_info', 'season']
    search_joined_values_list = ['spring']

    from util_for_clipvitb32 import clip_vit_b32, get_embedding
    clip_model = clip_vit_b32()
    search_embs = [get_embedding(clip_model, item) 
                            for item in search_joined_values_list]
    # 将search_embs中的每个tensor转成list，便于序列化
    search_embs = [emb.tolist() if hasattr(emb, 'tolist') else emb for emb in search_embs]

    threshold=0.8

    device="cuda:0"

    import requests
    url = "http://localhost:20022/satisfy_sim"
    payload = {
        "target_json_dict_str": target_json_dict_str,
        "non_empty_keys": non_empty_keys,
        "search_joined_values_list": search_joined_values_list,
        "search_embs": search_embs,
        "threshold": threshold,
        "device": device
    }
    r = requests.post(url, json=payload)
    print("Status code:", r.status_code)
    print("Response:", r.json())


if __name__ == '__main__':
    import os
    os.environ['GRADIO_TEMP_DIR'] = '/mnt/nas/shengjie/tmp'
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--cuda', type=str, default='0', help='CUDA device index or device string, eg "0" or "cuda:0"')
    parser.add_argument('-p', '--port', type=int, default=20023, help='gradio port')
    args, unknown = parser.parse_known_args()

    os.environ['CUDA_VISIBLE_DEVICES'] = args.cuda

    # app.run(host="0.0.0.0", port=args.port)
    test()

# 使用以下命令用 4 个 worker 开启 gunicorn 运行 Flask 应用
# (假定flask_retrival.py中的app对象名为app)
# 在终端中运行:
# gunicorn -w 4 -b 0.0.0.0:20022 flask_retrival:app

